package com.ruoyi.message.message;

import org.springframework.http.HttpHeaders;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 握手拦截器（处理会话）
 */
@Component
public class ChatHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response,
                                   WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) {
        // 获取HTTP Session中的用户信息
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            // 通过请求路径判断连接类型 是 chat 还是 notice
            String path = servletRequest.getURI().getPath();
            String type = path.substring(path.lastIndexOf('/') + 1);
            if (Objects.equals("chat", type)) {
                // 通过请求头传
                HttpHeaders headers = request.getHeaders();
                List<String> list = headers.get("senderId");
                List<String> list1 = headers.get("receiverId");
                List<String> list2 = headers.get("userType");
                if (!CollectionUtils.isEmpty(list) && !Objects.equals("[object Null]", list.get(0))) {
                    attributes.put("senderId", Long.parseLong(list.get(0)));
                }
                if (!CollectionUtils.isEmpty(list1) && !Objects.equals("[object Null]", list.get(0))) {
                    attributes.put("receiverId", Long.parseLong(list1.get(0)));
                }
                if (!CollectionUtils.isEmpty(list2) && !Objects.equals("[object Null]", list.get(0))) {
                    attributes.put("userType", list2.get(0));
                }
                // 通过query传
                String query = servletRequest.getURI().getQuery();
                if (Objects.nonNull(query) && query.contains("senderId") && query.contains("receiverId") && query.contains("userType")) {
                    String[] split = query.split("&");
                    for (String s : split) {
                        if (s.contains("senderId")) {
                            String[] split1 = s.split("=");
                            attributes.put("senderId", Long.parseLong(split1[1]));
                        }
                        if (s.contains("receiverId")) {
                            String[] split1 = s.split("=");
                            attributes.put("receiverId", Long.parseLong(split1[1]));
                        }
                        if (s.contains("userType")) {
                            String[] split2 = s.split("=");
                            attributes.put("userType", Long.parseLong(split2[1]));
                        }
                    }
                }
            } else if (Objects.equals("notice", type)) {
                // 通过请求头传
                HttpHeaders headers = request.getHeaders();
                List<String> list = headers.get("receiverId");
                if (!CollectionUtils.isEmpty(list) && !Objects.equals("[object Null]", list.get(0))) {
                    attributes.put("receiverId", Long.parseLong(list.get(0)));
                }
                List<String> list1 = headers.get("userType");
                if (!CollectionUtils.isEmpty(list1) && !Objects.equals("[object Null]", list1.get(0))) {
                    attributes.put("userType", Long.parseLong(list1.get(0)));
                }
                // 通过query传
                String query = servletRequest.getURI().getQuery();
                if (Objects.nonNull(query) && query.contains("receiverId")&& query.contains("userType")) {
                    String[] split = query.split("&");
                    for (String s : split) {
                        if (s.contains("receiverId")) {
                            String[] split1 = s.split("=");
                            attributes.put("receiverId", Long.parseLong(split1[1]));
                        }
                        if (s.contains("userType")) {
                            String[] split1 = s.split("=");
                            attributes.put("userType", Long.parseLong(split1[1]));
                        }
                    }
                }
            }
            
        }
        return true;
    }
}